package com.example.smartcity_poverty.ui.AllService.LivingPayment

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.LinearLayout
import android.widget.RadioButton
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.LivingPayment.LpAddAllListModel
import com.example.smartcity_poverty.logic.ServiceBean.LivingPayment.LpAddPhoneModel
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.LivingPayment.activity.LpAddActivity
import com.example.smartcity_poverty.ui.AllService.LivingPayment.adapter.LpAddPhoneAllAdapter
import kotlinx.android.synthetic.main.activity_living_payment.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope

/**
 * 生活缴费模块开发
 */
class LivingPaymentActivity : AppCompatActivity() {

    var phoneName: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_living_payment)
        title = "生活缴费"

        // 选择
        lpRadioGroup.setOnCheckedChangeListener { radioGroup, i ->
            val id = findViewById<RadioButton>(i)
            phoneName = id.text.toString()
        }


        lpHomeOk.setOnClickListener {
            if (phoneName != null) {
                if (lpHomePhone.text.toString().length >= 11) {
                    startActivity(Intent(this, LpAddActivity::class.java).apply {
                        putExtra("phoneName", phoneName.toString())
                        putExtra("phoneNumber", lpHomePhone.text.toString())
                    })
                } else "请输入正确的手机号！".show()
            } else "请选择运营商！".show()
        }

    }


    /**
     * 刷新缴费记录
     */
    override fun onStart() {
        super.onStart()
        val arrayAddPhone = ArrayList<LpAddAllListModel.RowsBean>()
        // 开启协程
        coroutine(Dispatchers.Main) {
            // 清除集合内容
            arrayAddPhone.clear()
            // 判断用户是否已经登录
            if (getToken().isNotEmpty()) {
                api.getLpAddAllList(getToken()).apply {
                    if (this.code == 200) {
                        // 添加数据到集合
                        arrayAddPhone.addAll(this.rows!!)

                        runOnUiThread {
                            // recyclerView适配器
                            lpHomeAddPhoneAllRv.apply {
                                this.isNestedScrollingEnabled = false
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter =
                                    LpAddPhoneAllAdapter(arrayAddPhone, R.layout.re_lp_add_phone)
                                // 刷新数据
                                this.adapter?.notifyItemChanged(arrayAddPhone.size)
                            }
                        }
                    } else runOnUiThread { this.msg?.show() }
                }
            } else runOnUiThread { "请先登录，查询缴费记录！".show() }
        }


    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}